//  Copyright (c) 2020-present,  INSPUR Co, Ltd.  All rights reserved.
// This source code is licensed under Apache 2.0 License.

#pragma once

#include "memory_arena_index.h"
#include "pure_mem/memoryblock/memory_arena/memory_arena.h"
#include "pure_mem/vector_index.h"
#include "rocksdb/slice.h"

namespace rocksdb {
class MemArenaVectorIndex : public MemArenaIndex {
public:
    explicit MemArenaVectorIndex() : MemArenaIndex() {};
    ~MemArenaVectorIndex() = default;

    IMemoryArena* SeekForMemoryArena(const Slice &key) override;
    bool Insert(const Slice& startKey, IMemoryArena* memoryArena) override;

private:
    Vector memory_arena_vector;
};

IMemoryArena * MemArenaVectorIndex::SeekForMemoryArena(const Slice &key) {
    assert(!key.empty());
    int32_t id = memory_arena_vector.SeekForPrev(key);
    if (id >= 0) {
        return  (IMemoryArena *)memory_arena_vector.GetMemArenaAddr(id);
    } else {
        return nullptr;
    }
}

bool MemArenaVectorIndex::Insert(const Slice &startKey, IMemoryArena *memoryArena) {
    Node n = Node(startKey, memoryArena);
    bool res = memory_arena_vector.Insert(n);
    return res;
}
}
